﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// GoodsInput.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsPrint : Window
    {
        private List<Visual> list = new List<Visual>();
        private DataTable dt = null;
        private int count = 1;

        public GoodsPrint(DataTable dt)
        {
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString() );
            InitializeComponent();
            this.dt = dt;
        }


        public void AddUI(UIElement element)
        {
            WrapPanel wp = new WrapPanel();
            wp.Background = Brushes.White;
            wp.Children.Add(element);
            wp.Margin = new Thickness(0, 5, 0, 0);
            this.panel.Children.Add(wp);
            this.list.Add(element);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() == true)
            {
                PrintDocumentPaginator docPaginator = new PrintDocumentPaginator(this.list);
                docPaginator.PageSize = new Size(310, 160);
                dialog.PrintDocument(docPaginator, "库存管理系统");
                this.Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < count; i++)
                    {
                        byte[] bytes = CodeBarHelper.Generate(dr["code"].ToString(), 300, 150);
                        BitmapImage bim = new BitmapImage();
                        bim.BeginInit();
                        bim.StreamSource = new MemoryStream(bytes);
                        bim.EndInit();
                        Image image = new Image();
                        image.Margin = new Thickness(5);
                        image.Source = bim;
                        AddUI(image);
                    }
                }
            }
        }

        private void cbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string tag = ((ComboBoxItem)this.cbx.SelectedItem).Tag as string;
            if (tag != null)
            {
                this.count = (int)Utils.ParseInt(tag);

                this.panel.Children.Clear();
                this.list.Clear();

                Window_Loaded(null, null);
            }
        }
    }
}
